package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entily.WmChannel;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;

import org.springframework.stereotype.Service;

/**
 * @BelongsProject: heima-leadnews
 * @BelongsPackage: com.itheima.media.service.impl
 * @Author: yanhongwei
 * @CreateTime: 2022-06-25  16:15
 * @Description: TODO
 * @Version: 1.0
 */
@Service

public class IWmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
      //判断是否为空
        if (wmChannel !=null) {
            // 名字是否存在
            if (StringUtils.isNotBlank(wmChannel.getName())) {
                LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(WmChannel::getName, wmChannel.getName());
                //根据名称判断这个频道是否已经存在
                int count = this.count(lambdaQueryWrapper);
                if (count > 0) {
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
                }
                //不存在 ;  保存
                this.save(wmChannel);
                return ResponseResult.okResult();
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }


    @Override
    public ResponseResult pageList(ChannelDto channelDto) {
        if(channelDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
     LambdaQueryWrapper<WmChannel> lambdaQueryWrapper =  new LambdaQueryWrapper();

        //name 为空
      //  if(channelDto.getName()==null)  获取name属性,如果为null,那么返回空字符串""
   //  1.StringUtils.isNotBlank(Object) 判断对象是否为空，包括为null，size=0，空格
       // 2.StringUtils.isNotEmpty(Object)判断对象是否为空，包括为null，size=0
        if(StringUtils.isNotBlank( channelDto.getName())){
            //对 name 进行模糊查询
            lambdaQueryWrapper.eq(WmChannel::getName,channelDto.getName());
        }
        //进行分页查询
        //        构建分页插件
        IPage<WmChannel> pageInfo = new Page<>(channelDto.getPage(),channelDto.getSize());
        IPage<WmChannel> page1 = this.page(pageInfo,lambdaQueryWrapper);
        //       返回结果 ResponseResult Long currentPage, Integer size, Long total, T data
        //当前页码 每页条数 总数
        return new PageResponseResult(channelDto.getPage(),channelDto.getSize(),page1.getTotal(),page1.getRecords());

    }
}
